package com.mallcloud.mall.mall.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.core.util.ValidGroup;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.mall.api.entity.MallImageGroupModel;
import com.mallcloud.mall.mall.service.MallImageGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "mall-图片组")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/mall/imagesGroup")
public class MallImagesGroupController {
	private final MallImageGroupService imageGroupService;

	@ApiOperation("分页")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('mall_imagesgroup_view')")
	public R<IPage<MallImageGroupModel>> page(Page page, MallImageGroupModel imageGroupModel) {
		return R.ok(imageGroupService.page(page, Wrappers.lambdaQuery(imageGroupModel)
				.orderByDesc(MallImageGroupModel::getCreateTime)
		));
	}

	@ApiOperation("组id列表")
	@GetMapping("/groupIds")
	@PreAuthorize("@pms.hasPermission('mall_imagesgroup_view')")
	public R<List<Long>> getGroupIds() {
		return R.ok(imageGroupService.getGroupIds());
	}

	@ApiOperation("查询详情")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('mall_imagesgroup_view')")
	public R<MallImageGroupModel> getById(@PathVariable("id") Long id) {
		return R.ok(imageGroupService.getOne(Wrappers.<MallImageGroupModel>lambdaQuery()
				.eq(MallImageGroupModel::getId, id)
		));
	}

	@ApiOperation("新增")
	@PostMapping("/save")
	@PreAuthorize("@pms.hasPermission('mall_imagesgroup_add')")
	public R<Boolean> save(@RequestBody MallImageGroupModel imageGroupModel) {
		return R.ok(imageGroupService.save(imageGroupModel));
	}

	@ApiOperation("修改")
	@PostMapping("/update")
	@PreAuthorize("@pms.hasPermission('mall_imagesgroup_edit')")
	public R<Boolean> updateById(@Validated({ValidGroup.Update.class}) @RequestBody MallImageGroupModel imageGroupModel) {
		return R.ok(imageGroupService.updateById(imageGroupModel));
	}

	@ApiOperation("删除")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('mall_imagesgroup_del')")
	public R<Boolean> removeById(@PathVariable Long id) {
		return R.ok(imageGroupService.remove(Wrappers.<MallImageGroupModel>lambdaUpdate()
				.eq(MallImageGroupModel::getId, id)
		));
	}

	@ApiOperation("批量删除")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('mall_imagesgroup_del')")
	public R<Boolean> removeById(@RequestBody List<Long> ids) {
		return R.ok(imageGroupService.remove(Wrappers.<MallImageGroupModel>lambdaUpdate()
				.in(MallImageGroupModel::getId, CollUtil.toList(ids))
		));
	}

	@ApiOperation("导出excel")
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('mall_imagesgroup_export')")
	public List<MallImageGroupModel> export(MallImageGroupModel imageGroupModel) {
		return imageGroupService.list(Wrappers.query(imageGroupModel));
	}
}
